//局部导入dayjs
import 'dayjs/locale/zh-cn'
import dayjs from 'dayjs';
dayjs.locale('zh-cn')

function countLetterOccurrences(str) {
    let arr = str.split('');
    let yearArr = arr.filter(item => {
        return item == 'y'
    })
    let year = yearArr.join("")
    let monthArr = arr.filter(item => {
        return item == 'M'
    })
    let month = monthArr.join("")
    
    let dayArr = arr.filter(item => {
        return item == 'd'
    })
    let day = dayArr.join("")
    let hourArr = arr.filter(item => {
        return item == 'H'
    })
    let hour = hourArr.join("")
    let minuteArr = arr.filter(item => {
        return item == 'm'
    })
    let minute = minuteArr.join("")
    let secontArr = arr.filter(item => {
        return item == 's'
    })
    let secont = secontArr.join("")
    let Y = yearArr.length > 0 ? year.toUpperCase() + "-" : "";
    let M = monthArr.length > 0 ? month.toUpperCase() + "-" : "";
    let D = dayArr.length > 0 ? day.toUpperCase() + "-" : "";
    let H = hourArr.length > 0 ? hour.toUpperCase() + "-" : "";
    let m = minuteArr.length > 0 ? minute + "-" : "";
    let s = secontArr.length > 0 ? secont: "";
    return Y + M + D + H + m + s

}

export function useOrderNum(str) {
    console.log(str)
    let rs = countLetterOccurrences(str)
    let result = dayjs(new Date()).format(rs);
    return result
}

// 计算两个时间差
export function differenceInTime(time1, time2){
    const date1 = dayjs(time1);
    const date2 = dayjs(time2);
    const differenceInMinutes = date2.diff(date1, 'minute');
    return differenceInMinutes;
}

// 格式化日期
export function getConvertDateTimeShort(date) {
    return dayjs(date).format("YYYY-MM-DD HH:mm:ss")
}

export function getConvertDateShort(date) {
    return dayjs(date).format("YYYY-MM-DD")
}

// 获取当前日期年月日,时间格式为YYYY-MM-DD
export function getCurrentDate() {
    return dayjs(new Date()).format("YYYY-MM-DD")
}

// 获取当前日期年月日,时间格式为YYYY-MM-DD HH:mm:ss
export function getCurrentTime() {
    return dayjs(new Date()).format("YYYY-MM-DD HH:mm:ss")
}

// 判断是否过期
export function isBefore(date1, date2) {
    return dayjs(date1).isBefore(dayjs(date2))
}

// 当前时分秒
export function getBehindTime(){
    return dayjs(new Date()).format('HH:mm:ss')
}

// 获取几天后的日期
export function getFutureDate(date,n){
    return dayjs(date).add(n, 'day').format('YYYY-MM-DD');
}

// 下载工艺单，附件
export function handleDown(num, proxy, path, fileName, pName, orderId,n) {
    let type = path.substring(path.lastIndexOf('.') + 1).toLowerCase();
    const data = { filePath: path,orderId:orderId,type:num == 1 ? 0:1 };
        proxy.download(
            "/system/file/downloadForOrder",
            { ...data },
            (n ? pName : fileName) + `_${getCurrentDate()}.` + type
        );
}

// 点击图标跳转工艺单,附件
export function viewFile(router, file, id){
    const routeUrl = router.resolve({ path: '/preview', query: { id: id, file: file } });
    window.open(routeUrl.href, '_blank');
}

// 一个月
export function getClosestMonth() {
    // 获取当前日期
    const currentDate = dayjs();

    // 获取一个月前的日期
    const oneMonthAgo = currentDate.subtract(1, 'month');

    // 格式化日期
    const formattedCurrentDate = currentDate.format('YYYY-MM-DD');
    const formattedOneMonthAgo = oneMonthAgo.format('YYYY-MM-DD');
    return {
        start: formattedOneMonthAgo,
        end: formattedCurrentDate,
    };
}

// 生产
export function handleSubmitView(code){

}

// 选中输入框
export function focusInput (inputValue) {
    nextTick(() => {
        const elInput = inputValue?.input;
        if (elInput) {
            elInput.focus();
        }
    });
}
